package com.jzo2o.market.handler;

import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.market.service.ICouponService;
import com.jzo2o.redis.handler.SyncProcessHandler;
import com.jzo2o.redis.model.SyncMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

import static com.jzo2o.market.constants.RedisConstants.RedisKey.COUPON_SEIZE_SYNC_QUEUE_NAME;

/**
 * 抢卷同步队列同步处理器
 * @author JIAN
 */
@Slf4j
@Component(COUPON_SEIZE_SYNC_QUEUE_NAME)
public class SeizeCouponSyncProcessHandler implements SyncProcessHandler<Object> {
    @Resource
    private ICouponService couponService;

    @Override
    public void batchProcess(List<SyncMessage<Object>> multiData) {
        throw new CommonException("不支持批量处理");
    }

    @Override
    public void singleProcess(SyncMessage<Object> singleData) {
        long userId = Long.parseLong(singleData.getKey());
        long activityId = Long.parseLong(singleData.getValue().toString());

        log.info("同步优惠卷信息, 活动id: {}, 用户id: {}", activityId, userId);
        couponService.syncCouponRecord(activityId, userId);
    }
}